local Msg = require("core.Msg")
local DailyActFW = require("activity.DailyActFW")
local ActivityExcel = require("excel.Activity")
local DailyActConfig = ActivityExcel.dailyAct
local WildBossConfig = ActivityExcel.wildboss
local ActDefine = require("activity.Define")
local ActRule = ActivityExcel.dailyActRule
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")

local function get_grid(itemID, itemNum, itemColor, itemBind)
	local grid = Grid.create(nil, itemID, itemNum, itemBind, ItemDefine.qualityOut[itemColor])
	return grid
end

function CG_ACTIVITY_CONFIG_QUERY(human, msg)
    local nActType = msg.actType
    local nActNum = msg.actNum
    local nActConfigID = msg.actConfigID
    if nActType ~= ActDefine.ACT_TYPE_WILDBOSS then
        local nActList = DailyActFW.DailyActList
        if nActList and nActList[nActType] then
            local nActData = nActList[nActType]._configList[nActConfigID][nActNum]
            if not nActData then
                return
            end
            local nActConfigData = DailyActConfig[nActConfigID]
            local nActRule = ActRule[nActData.ruleid]
            if nActConfigData and nActRule then
                local nMsg = Msg.gc.GC_ACTIVITY_CONFIG_QUERY
                nMsg.actType = nActType
                nMsg.actNum = nActNum
                nMsg.desc1 = nActConfigData.desc1
                nMsg.desc2 = nActConfigData.desc2
                nMsg.actReward[0] = 0
                for k, v in pairs(nActRule.item) do
                    nMsg.actReward[0] = nMsg.actReward[0] + 1
                    local nRewardData = nMsg.actReward[nMsg.actReward[0]]
                    nRewardData.key = k
                    nRewardData.itemData[0] = 0
                    for kk, vv in ipairs(v) do
                        nRewardData.itemData[0] = nRewardData.itemData[0] + 1 
                        local nItemData = nRewardData.itemData[nRewardData.itemData[0]]
                        local grid = get_grid(vv[1], vv[2], vv[3], vv[4] or 0)
                        Grid.makeItem(grid, nItemData)
                    end
                end
                Msg.send(nMsg, human.fd)
            end
        end
    else
        local nWildBossConfig = WildBossConfig[nActNum]
        if not nWildBossConfig then
            return
        end

        local nMsg = Msg.gc.GC_ACTIVITY_CONFIG_QUERY
        nMsg.actType = nActType
        nMsg.actNum = nActNum
        nMsg.desc1 = nWildBossConfig.desc1
        nMsg.desc2 = nWildBossConfig.desc2 or ""
        nMsg.actReward[0] = 0
        for k, v in pairs(nWildBossConfig.item) do
            nMsg.actReward[0] = nMsg.actReward[0] + 1
            local nRewardData = nMsg.actReward[nMsg.actReward[0]]
            nRewardData.key = k
            nRewardData.itemData[0] = 0
            for kk, vv in ipairs(v) do
                nRewardData.itemData[0] = nRewardData.itemData[0] + 1 
                local nItemData = nRewardData.itemData[nRewardData.itemData[0]]
                local grid = get_grid(vv[1], vv[2], vv[3], vv[4] or 0)
                Grid.makeItem(grid, nItemData)
            end
        end
        Msg.send(nMsg, human.fd)
    end
end
